// @ts-nocheck
import session from "koa-session2";
const Koa = require("koa");
const cors = require('koa-cors');
const compress = require('koa-compress');
const json = require('koa-json');
const views = require("koa-views");
const serve = require("koa-static");
const logger = require('koa-logger');
const convert = require('koa-convert');
const body = require('koa-better-body');
const onerror = require('koa-onerror');
const favicon = require('koa-favicon');
const router = require("koa-router")();
const path = require("path");

import index from "./routes/index";

const app = new Koa();
onerror(app);
app.use(function (ctx, next) {
    ctx.state = ctx.state || {};
    ctx.state.now = new Date();
    ctx.state.ip = ctx.ip;
    ctx.state.version = '2.0.0';
    return next();
});
// 设置gzip
app.use(compress({
    threshold: 2048,
    flush: require('zlib').Z_SYNC_FLUSH
}))
app.use(convert(logger()));
app.use(session({
    //store: new Store(),
    //cookie的保存期为一天
    maxAge: 1000 * 60 * 60 * 24,
}));
// 设置渲染引擎
app.use(views(__dirname + '/views', {//这里应该是包含了ejs和别的一些，这里把扩展给限定为ejs
    extension: 'ejs'
}));
app.use(convert(serve(path.join(__dirname + '/public/'))));
app.use(index.routes());   /*启动路由*/
app.use(async (ctx) => {
    if (ctx.status === 404) {
        await ctx.render('./error/404');
    }
});
if (process.env.NODE_ENV === 'test') {
    module.exports = app.callback();
} else {
    app.listen(3000);
    console.log('open http://localhost:3000');
}

